package com.aliengen.thingple.driverapp.ui.adapters;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.aliengen.thingple.driverapp.R;
import com.aliengen.thingple.driverapp.data.models.CylinderTag;
import com.aliengen.thingple.driverapp.ui.fragments.CylindersListFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * This class is part of the Thingple mobile application for the CMC168 Handheld reader.
 *
 * Copyright AlienGen Limited 2016. All rights reserved.
 *
 * @author Timothé Mermet-Buffet <tim@aliengen.com>
 */
public class CylindersListAdapter  extends RecyclerView.Adapter<CylindersListAdapter.ViewHolder> {
    private List<CylinderTag> mValues;
    private final CylindersListFragment.OnListFragmentListener mListener;

    public CylindersListAdapter(CylindersListFragment.OnListFragmentListener listener) {
        mValues = new ArrayList<CylinderTag>();
        mListener = listener;
    }

    public void updateList(HashMap<String, CylinderTag> values)
    {
        mValues = new ArrayList<CylinderTag>(values.values());
        notifyDataSetChanged();
    }

    public void updateList(List<CylinderTag> values)
    {
        mValues = values;
        notifyDataSetChanged();
    }

    public List<CylinderTag> getList()
    {
        return mValues;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapter_cylinder, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        CylinderTag item = mValues.get(position);
        holder.mItem = item;
        holder.mIdView.setText(new Integer(position+1).toString());
        holder.mReferenceView.setText(item.getReference());
        holder.mBundleView.setText(item.getBundleReference());
        holder.mStatusView.setText(item.getStatus());

        /*
        holder.mButtonView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mListener) {
                    // Notify the active callbacks interface (the activity, if the
                    // fragment is attached to one) that an item has been selected.
                    mListener.onListInteraction(holder.mItem);
                }
            }
        });
        */
    }

    @Override
    public int getItemCount() {
        return mValues.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public final View mView;
        public final TextView mIdView;
        public final TextView mReferenceView;
        public final TextView mBundleView;
        public final TextView mStatusView;
        //public final Button mButtonView;
        public CylinderTag mItem;

        public ViewHolder(View view) {
            super(view);
            mView = view;
            mIdView = (TextView) view.findViewById(R.id.id);
            mReferenceView = (TextView) view.findViewById(R.id.reference);
            mBundleView = (TextView) view.findViewById(R.id.bundle);
            mStatusView = (TextView) view.findViewById(R.id.status);
            //mButtonView = (Button) view.findViewById(R.id.button_view);
        }

        @Override
        public String toString() {
            return super.toString() + " '" + mReferenceView.getText() + "'";
        }
    }
}
